﻿// 功能：
// 描述：
// 作者：温伟鹏
// 日期：2010/10/22 15:04:00

using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using MeizuSDK.Drawing;

namespace GoogleMaps
{
    /// <summary>
    /// 在地图上显示的路径
    /// </summary>
    public class MapPath
    {
        #region 常量
        /// <summary>
        /// 默认路径线条宽度。单位：像素
        /// </summary>
        public const byte DefaultWeight = 5;
        /// <summary>
        /// 默认路径线条的颜色
        /// </summary>
        public static readonly Color DefaultColor = Color.Red;
        #endregion

        #region 私有变量
        /// <summary>
        /// 路径点集合
        /// </summary>
        private List<GeoPoint> locationList;
        /// <summary>
        /// 路径线条的透明度
        /// </summary>
        private int opacity;
        #endregion

        #region 公共属性
        /// <summary>
        /// 获取一个值，指示此路径是否为空路径。
        /// </summary>
        public bool IsEmpty
        {
            get
            {
                return locationList.Count < 2;
            }
        }
        /// <summary>
        /// 获取或设置路径线条的宽度。单位：像素。默认为5像素
        /// </summary>
        public byte Weight
        {
            get;
            set;
        }
        /// <summary>
        /// 获取或设置路径线条的颜色
        /// </summary>
        public Color Color
        {
            get;
            set;
        }
        /// <summary>
        /// 获取或设置叠加层的填充颜色。用于表明路径对某个多边形区域进行划分
        /// </summary>
        public Color FillColor
        {
            get;
            set;
        }
        /// <summary>
        /// 获取或设置路径线条的透明度。范围：-1-255。255表示完全不透明。-1表示不设置透明度
        /// </summary>
        public int Opacity
        {
            get
            {
                return this.opacity;
            }
            set
            {
                if (value != this.opacity)
                {
                    if (value > 255 || value < -1)
                    {
                        throw new ArgumentOutOfRangeException("value", "透明度设置超出范围。范围：-1-255。255表示完全不透明。-1表示不设置透明度");
                    }

                    this.opacity = value;
                }
            }
        }
        #endregion

        #region 构造函数
        /// <summary>
        /// 构造地图路径实例
        /// </summary>
        /// <param name="color">线条颜色</param>
        public MapPath(Color color)
            : this(DefaultWeight, color)
        {

        }
        /// <summary>
        /// 构造地图路径实例
        /// </summary>
        /// <param name="weight">路径线条宽度</param>
        /// <param name="color">线条颜色</param>
        public MapPath(byte weight, Color color)
            : this(weight, color, Color.Empty)
        {

        }
        /// <summary>
        /// 构造地图路径实例
        /// </summary>
        /// <param name="weight">路径线条宽度</param>
        /// <param name="color">线条颜色</param>
        /// <param name="fillColor">线条分割区域的填充颜色。设置为Color.Empty表示不填充</param>
        public MapPath(byte weight, Color color, Color fillColor)
            : this(weight, color, fillColor, -1)
        {

        }
        /// <summary>
        /// 构造地图路径实例
        /// </summary>
        /// <param name="weight">路径线条宽度</param>
        /// <param name="color">线条颜色</param>
        /// <param name="fillColor">线条分割区域的填充颜色。设置为Color.Empty表示不填充</param>
        /// <param name="opacity">路径线条的透明度。范围：-1-100。100表示完全不透明。-1表示不设置透明度</param>
        public MapPath(byte weight, Color color, Color fillColor, int opacity)
        {
            Weight = weight;
            Color = color;
            FillColor = fillColor;
            Opacity = opacity;
            locationList = new List<GeoPoint>();
        }
        #endregion

        #region 函数
        /// <summary>
        /// 添加路径拐点
        /// </summary>
        /// <param name="point"></param>
        public void AddPoint(GeoPoint point)
        {
            if (point != null)
            {
                locationList.Add(point);
            }
        }
        /// <summary>
        /// 返回描述在地图上显示的路径的字符串
        /// </summary>
        /// <returns></returns>
        public override string ToString()
        {
            if (IsEmpty)
                return string.Empty;

            StringBuilder path = new StringBuilder();
            path.Append("path=");

            // 颜色
            if (Color == null || Color.IsEmpty)
            {
                Color = DefaultColor;
            }
            path.Append(string.Format("color:{0}|", GetColorString(Color)));

            // 线宽
            path.Append(string.Format("weight:{0}|", Weight));

            // 填充颜色
            if (Color != null && !Color.IsEmpty)
            {
                path.Append(string.Format("fillcolor:{0}|", GetColorString(FillColor)));
            }

            // 拐点坐标
            for (int i = 0; i < locationList.Count; i++)
            {
                GeoPoint point = locationList[i];

                if (i == 0)
                {
                    path.Append(point.ToString());
                }
                else
                {
                    path.Append("|" + point.ToString());
                }
            }

            return path.ToString();
        }
        /// <summary>
        /// 获取颜色字符串。如果存在透明度设置，则格式为：0xffffff00，最后两位表示透明度，范围00-FF
        /// </summary>
        /// <param name="color"></param>
        /// <returns></returns>
        private string GetColorString(Color color)
        {
            bool useAlpha = (Opacity != -1);

            string result = ColorTranslator.ToHex(color);

            if(useAlpha)
            {
                result += Opacity.ToString("X2", null);
            }

            return result.ToLower();
        }
        #endregion
    }
}
